﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TestShderFade : MonoBehaviour
{

    [Range(0, 1)]
    public float leftX = 0;
    [Range(0, 1)]
    public float rightX = 0;
    [Range(0, 1)]
    public float topY = 0;
    [Range(0, 1)]
    public float bottomY = 0;
    [Range(-2, 0)]
    public float alphaSmooth = 0;
    // Use this for initialization
    public Shader imageShader;
    private Material _materal;
    public Image image;
    public float delayTime;

    public Material _Material
    {
        get
        {
            _materal = image.GetComponent<Image>().material;
            return _materal;
        }
    }

    private void Awake()
    {
        imageShader = Shader.Find("Unlit/ImageAlpha");
        image = this.GetComponent<Image>();
        leftX = 0.1f;
        alphaSmooth = 0.1f;
    }

    private void Start()
    {
        StartCoroutine(ChangeAlpha());
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    public IEnumerator ChangeAlpha()
    {
        while (delayTime < 3f)
        {
            yield return new WaitForEndOfFrame();
            leftX += Time.deltaTime*0.5f;

            _Material.SetFloat("_AlphaLX", leftX * 2);
            _Material.SetFloat("_AlphaRX", ((1 - rightX) - 0.5f) * 2);
            _Material.SetFloat("_AlphaTY", ((1 - topY) - 0.5f) * 2);
            _Material.SetFloat("_AlphaBY", bottomY * 2);
            _Material.SetFloat("_AlphaPower", alphaSmooth);

            delayTime = delayTime + Time.deltaTime;
            if (delayTime >= 3f)
            {
                StopCoroutine("ChangeAlpha");
            }
        }
    }
}
